package com.dai.zhenfujin.visitingcard.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.FansImagesAdpter;
import com.dai.zhenfujin.adapter.PersonalCommentAdapter;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.AlumFansListBean;
import com.dai.zhenfujin.bean.CommentListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.view.SodukuGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentDetailsActivity extends BaseActivity implements View.OnClickListener {

    private CircleImageView cv_img;
    private SodukuGridView grid_view;
    private RecyclerView rl_new_comment;
    private FrameLayout fl_img;
    public  EditText et_send_comment;
    private Button bt_send;
    private TextView tv_name,item_time,tv_connect,title_textview,tv_comment,pp_adapter_position,pp_adapter_bloc,item_word_type;
    private AlumFansListBean.MsgListBean itemList;
    private List<CommentListBean.ComListBean> com_list = new ArrayList<>();
    private PersonalCommentAdapter personalCommentAdapter;
    public static InputMethodManager inputMethodManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment_details_activity);
        itemList = (AlumFansListBean.MsgListBean)getIntent().getSerializableExtra("itemList");
        inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        initView();

    }

    private void initView() {
        cv_img = findViewById(R.id.cv_img);
        tv_name = findViewById(R.id.tv_name);
        item_time = findViewById(R.id.item_time);
        tv_connect = findViewById(R.id.tv_connect);
        tv_comment = findViewById(R.id.tv_comment);
        grid_view = findViewById(R.id.grid_view);
        rl_new_comment = findViewById(R.id.rv_new_comment);
        title_textview = findViewById(R.id.title_textview);
        fl_img = findViewById(R.id.fl_img);
        bt_send = findViewById(R.id.bt_send);
        pp_adapter_position = findViewById(R.id.pp_adapter_position);
        pp_adapter_bloc = findViewById(R.id.pp_adapter_bloc);
        item_word_type = findViewById(R.id.item_word_type);
        et_send_comment = findViewById(R.id.et_send_comment);
        fl_img.setOnClickListener(this);
        bt_send.setOnClickListener(this);

        title_textview.setText("福利详情");

        Glide.with(this).load(itemList.getUser_logo()).into(cv_img);
        tv_name.setText(itemList.getUser_name());
        item_time.setText(itemList.getCreate_time().substring(0,itemList.getCreate_time().length()-2));
        tv_connect.setText(itemList.getMsg_content());
        pp_adapter_position.setText(itemList.getUser_duties());
        pp_adapter_bloc.setText("公司:" + itemList.getUser_company());
        item_word_type.setText("主业:" + itemList.user_job);

        if (itemList.getUrl_list().size() > 0) {
            List<String> list = itemList.getUrl_list();
            FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(this,list);
            grid_view.setAdapter(fansImagesAdpter);
        }

        rl_new_comment.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        personalCommentAdapter = new PersonalCommentAdapter(CommentDetailsActivity.this,com_list,et_send_comment);
        rl_new_comment.setAdapter(personalCommentAdapter);
        getCommentList();
    }

    private void getCommentList() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("msg_id", itemList.getMsg_id());
        params.put("req_bd",  params2);
        HttpClient.post(this, Constants.COMMENTLIST, params, new CallBack<CommentListBean>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(CommentListBean resultBean) {
                com_list.clear();
                com_list.addAll(resultBean.getCom_list());
                tv_comment.setText("全部评论(" + com_list.size() + "条)");
                personalCommentAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_img:
                finish();
                break;
            case R.id.bt_send:
                if (TextUtils.isEmpty(et_send_comment.getText().toString().trim())){
                    UIUtils.showToastSafe("请输入评论内容");
                    return;
                }
                sendComment();
                break;
        }
    }

    private void sendComment() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("com_content", et_send_comment.getText().toString().trim());
        params2.put("msg_id", itemList.getMsg_id());
        if (personalCommentAdapter.flag) {
            params2.put("p_user_id", personalCommentAdapter.user_id);
        }
        params.put("req_bd",  params2);
        HttpClient.post(this, Constants.COMMENT, params, new CallBack<CommentListBean>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(CommentListBean resultBean) {
                et_send_comment.setText("");
                getCommentList();
                personalCommentAdapter.flag = false;
                et_send_comment.setHint("请发表评论");
                hideSoftKeyboard();
            }
        });
    }

    private void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
